import logic.IMessageHandler;
import logic.ServerMessageHandler;
import logic.UserMessageHandler;
import network.message.MessageConstants;
import network.server.HttpServerProvider;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by IntelliJ IDEA.
 * User: grigory.rozhkov
 * Date: 11.09.12
 * Time: 23:03
 * To change this template use File | Settings | File Templates.
 */
public class Main {
    public static void main(String[] args) {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String userMessage = "";
        try {
            userMessage = bufferedReader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        while (!MessageConstants.EXIT.equals(userMessage)) {
            IMessageHandler userMessageHandler = new UserMessageHandler();
            IMessageHandler serverMessageHandler = new ServerMessageHandler();
            String userHandledMessage = userMessageHandler.handleMessage(userMessage);
            if (MessageConstants.FAIL.equals(userHandledMessage)) {
                System.out.println(MessageConstants.FAILURE_WRONG_MESSAGE);
            } else {
                HttpServerProvider httpServerProvider = new HttpServerProvider();
                try {
                    String serverResponse = httpServerProvider.sendMessage(userHandledMessage);
                    String handledServerResponse = serverMessageHandler.handleMessage(serverResponse);
                    System.out.println(serverResponse);
                } catch (IOException e) {
                    System.out.println(MessageConstants.PROBLEM_WITH_SERVER);
                }
            }
            try {
                userMessage = bufferedReader.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
